package net2;

import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;

public class TcpEchoServer {

    private static final int PORT = 4090;
    public static void main(String[] args) throws IOException {
        ServerSocket socket = new ServerSocket(PORT);
        System.out.println("服务器已启动...");
        while (true) {
            System.out.println("等待接收客户端消息数据...");
            Socket client = socket.accept();
            System.out.println("已获取来自客户端：" +
                    client.getPort() + "/" +
                    client.getInetAddress() + "的数据:");

            InputStream is = client.getInputStream();
            BufferedReader bf = new BufferedReader(
                    new InputStreamReader(is, "UTF-8"));
            String line = bf.readLine();
            System.out.println(line);

            OutputStream os = client.getOutputStream();
            PrintWriter pw = new PrintWriter(
                    new OutputStreamWriter(os, "UTF-8"));
            pw.println(line);
            pw.flush();
            System.out.println("已发送回显...");
            System.out.println("----------------------");
            client.close();
        }
    }

}